package lzupdater.utils;

import lzupdater.locale.LocalizedString;
import lzupdater.log.Logger;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * 
 * @author Fraskor
 */
public class Profile {

	private final static LocalizedString setInfoString = new LocalizedString(
			"Set '%s' to '%s'");

	private final Logger logger;

	/**
	 * Enum of the fields composing a profile. Each value holds parameters
	 * useful for the Profile manager
	 * 
	 * @author Fraskor
	 */
	public enum ProfilePropertyId {
		NAME("name", "", false), LOGIN("login", "", false), PASSWORD(
				"password", "", true), ROSTER_FILE("rosterFile", "", false), ROSTER_URL(
				"rosterUrl", "", false), SAVE_PASSWORD("passwordSaved",
				"false", false);

		private final boolean encryptedValue;
		private final String defaultValue;
		private final String path;

		private ProfilePropertyId(String path, String defaultValue, boolean flag) {
			encryptedValue = flag;
			this.defaultValue = defaultValue;
			this.path = path;
		}

		/**
		 * @return true if the properties must be encrypted when displayed
		 */
		public boolean isEncryptedValue() {
			return encryptedValue;
		}

		/**
		 * @return the default value of the property
		 */
		public String getDefaultValue() {
			return defaultValue;
		}

		/**
		 * @return the relative path of the property
		 */
		public String getPath() {
			return path;
		}
	}

	private String name;
	private String login = "";
	private String password = "";
	private String rosterFile = "";
	private RosterUrl rosterUrl = new RosterUrl("");
	private boolean savePassword = true;

	public Profile(Logger logger) {
		Validate.notNull(logger);
		this.logger = logger;
	}

	public Profile clone(Profile ref) {
		name = ref.name;
		login = ref.login;
		password = ref.password;
		rosterFile = ref.rosterFile;
		rosterUrl.clone(ref.rosterUrl);
		savePassword = ref.savePassword;
		return this;
	}

	public String getProperty(ProfilePropertyId id) {
		switch (id) {
		case NAME:
			return getName();
		case LOGIN:
			return getLogin();
		case PASSWORD:
			return getPassword();
		case ROSTER_FILE:
			return getRosterFile();
		case ROSTER_URL:
			return getRosterUrl();
		case SAVE_PASSWORD:
			return Boolean.toString(isSavePassword());
		default:
			return "";
		}
	}

	public void setProperty(ProfilePropertyId id, String value) {
		String oldValue = getProperty(id);

		switch (id) {
		case LOGIN:
			setLogin(value);
			break;
		case PASSWORD:
			setPassword(value);
			break;
		case ROSTER_FILE:
			setRosterFile(value);
			break;
		case ROSTER_URL:
			setRosterUrl(value);
			break;
		case SAVE_PASSWORD:
			setSavePassword(Boolean.parseBoolean(value));
			break;
		default:
			oldValue = value;
		}

		if (!oldValue.equals(value)) {
			String displayValue = id.encryptedValue ? Encrypter
					.getMaskedPassword(value) : value;
			String msg = setInfoString.useAsFormater(id, displayValue);
			logger.debug(msg);
		}

	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the login
	 */
	public String getLogin() {
		return login;
	}

	/**
	 * @param login
	 *            the login to set
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the rosterFile
	 */
	public String getRosterFile() {
		return rosterFile;
	}

	/**
	 * @param rosterFile
	 *            the rosterFile to set
	 */
	public void setRosterFile(String rosterFile) {
		this.rosterFile = rosterFile;
	}

	/**
	 * @return the rosterUrl
	 */
	public String getRosterUrl() {
		return rosterUrl.getUrl();
	}

	/**
	 * @param rosterUrl
	 *            the rosterUrl to set
	 */
	public void setRosterUrl(String rosterUrl) {
		this.rosterUrl.setUrl(rosterUrl);
	}

	/**
	 * @return the savePassword
	 */
	public boolean isSavePassword() {
		return savePassword;
	}

	/**
	 * @param savePassword
	 *            the savePassword to set
	 */
	public void setSavePassword(boolean savePassword) {
		this.savePassword = savePassword;
	}

}